package com.service;

import com.dao.UserDao;
import com.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    /*
     * 展示所用用户
     */
    public List<User> listUser() {
        return userDao.getALlUser();
    }

    /**
     * 事务管理,以及发生异常回滚事务。
     */
    @Transactional
    public void  addAge(){
        //先增加年龄
            userDao.updateUser(new User(1,"Linguo",15));

        //遇到异常
        throw  new RuntimeException("发生异常...");
    }

    /**
     * PageHelper分页
     * @param pageNum 页码
     * @return
     */
    public  List<User> findUser(int pageNum){
        List<User> userList  = userDao.getALlUser();
        return userList;
    }


    public  User findUserById(int id){
        User user = userDao.findById(id);
        return user;
    }

    @Transactional
    public int updateUser(User user){
        return userDao.updateUser(user);
    }

    @Transactional
    public int insertUser(User user){
        return userDao.insertUser(user);
    }






}
